﻿using System.Linq;
using System.Web.Mvc;
using SmallBusinessKit.Interfaces;
using SmallBusinessKit.ViewModels;

namespace SmallBusinessKit.Controllers
{
    public class ItemsController : BaseController
    {
        #region Proeprties

        private readonly ICatalogDomain _catalogDomain;

        #endregion

        #region Methods

        public ItemsController(ICatalogDomain catalogDomain)
        {
            _catalogDomain = catalogDomain;
        }

        [Route("Items")]
        public ViewResult Index()
        {
            var viewModel = new ItemsViewModel
            {
                Categories = _catalogDomain.GetCategories(null)
            };

            return View(viewModel);
        }

        [Route("Items/Category/{id}")]
        public PartialViewResult GetChildCategories(string id)
        {
            PartialViewResult partialView;
            var catList = _catalogDomain.GetCategories(id);

            if (catList.Any(i => i.ChildCategories.Any() && !i.Items.Any()))
            {
                partialView = PartialView("_Categories", catList);
            }
            else
            {
                partialView = PartialView("_SubCategories", catList);
            }

            return (partialView);
        }

        [Route("Items/SubCategory/{id}")]
        public PartialViewResult GetChildItems(string id)
        {
            var items = _catalogDomain.GetChildItems(id);

            var partialView = items.Any() ? PartialView("_Items", items) : PartialView("_NoItems");

            return partialView;
        }

        
        [Route("Items/Detail/{id}")]
        public ViewResult Detail(string id)
        {
            var item = _catalogDomain.GetItem(id);
            
            return View(item);
        }

        #endregion
    }
}